﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business_Objects;
using ServicesComponent.Repositories;

public partial class QuanLyHocSinh_DiemDanhDetails : System.Web.UI.Page
{
    private IDiemDanhRepository _DiemDanhRepository;
    private IClassRepository _ClassRepository;
    private IStudentRepository _StudentRepository;
    private static string id = "";
    public int Stt;
    private static string action;
    protected void Page_Load(object sender, EventArgs e)
    {
        Stt = Stt + 1;
        _DiemDanhRepository = FactoryServices.GetServicesDiemDanh(_DiemDanhRepository);
        action = Request.QueryString["ac"].ToString();
        id = Request.QueryString["id"].ToString();
        if (IsPostBack == false)
        {
            if (action == "'sua'")
            {
                GetDataToCombobox();
                DiemDanh obj = _DiemDanhRepository.Get(id);
                ddlKhoi.SelectedValue = obj.ClassID.ToString();
                txtNgayDD.Text = obj.NgayDiemDanh.ToShortDateString().ToString();
                ddlKhoi.Enabled = false;
                txtNgayDD.Enabled = false;
                GetDataRepeatEdit(ddlKhoi.SelectedValue);
                txtSiSo.Text = rptA.Items.Count.ToString();
                txtSiSo.Enabled = false;
            }
            if (action == "'them'")
            {
                GetDataToCombobox();
                txtNgayDD.Text = DateTime.Now.ToShortDateString();
                GetDataRepeatEdit(ddlKhoi.SelectedValue);
                txtSiSo.Text = rptA.Items.Count.ToString();
            }

        }
        //   txtSiSo.Value = rptA.Items.Count.ToString();
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        if (action == "'sua'")
        {
            //Khó có thẻ list source 1 trable - details 1 table
            foreach (RepeaterItem item in rptA.Items)
            {
                HiddenField hi = item.FindControl("HdField") as HiddenField;
                if (hi != null)
                {
                    DiemDanh obj = _DiemDanhRepository.GetDiemDanhBySchoolIdAndSchoolYearAndStudentId(Session["SchoolYearID"].ToString(),
                                                                                                      int.Parse(Session["SchoolID"].ToString()), hi.Value);
                    CheckBox ch = item.FindControl("ChBox") as CheckBox;
                    if (ch != null) ch.Checked = obj.Comat;
                }
            }
        }
        else
        {
            RepeaterItem ri = e.Item;
            var isDefaultCheckBox = ri.FindControl("ChBox") as CheckBox;
            if (isDefaultCheckBox != null) isDefaultCheckBox.Checked = true;
        }

    }
    private void GetDataToCombobox()
    {
        _ClassRepository = FactoryServices.GetServicesClass(_ClassRepository);
        ddlKhoi.DataSource = _ClassRepository.GetList();
        ddlKhoi.DataValueField = "Id";
        ddlKhoi.DataTextField = "Name";
        ddlKhoi.DataBind();
    }
    //Get edit
    void GetDataRepeatEdit(string classId)
    {
        _StudentRepository = FactoryServices.GetServicesStudent(_StudentRepository);
        rptA.DataSource = _StudentRepository.GetStudentBySchoolIdAndSchoolYearClass(Session["SchoolYearID"].ToString(),
                                                                     int.Parse(Session["SchoolID"].ToString()), classId);

        rptA.DataBind();

        Cpage.DataSource = _StudentRepository.GetStudentBySchoolIdAndSchoolYearClass(Session["SchoolYearID"].ToString(),
                                                                     int.Parse(Session["SchoolID"].ToString()), classId);
        Cpage.BindToControl = rptA;
        rptA.DataSource = Cpage.DataSourcePaged;
    }
    //get add = Edit - Cùng fill student in class

    protected void ddlKhoi_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetDataRepeatEdit(ddlKhoi.SelectedValue);
        txtSiSo.Text = rptA.Items.Count.ToString();
        //txtDenLop.Value = txtSiSo.Value;
        //txtVang.Value = "0";

    }

    bool ValiDate()
    {
        //if (txtMaNK.Value.ToString() == "" || txtTenNK.Value.ToString() == "" || txtNamNK.Value.ToString() == "")
        //{
        //    return false;
        //}
        return true;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int vang = 0;
        if (ValiDate())
        {

            if (action == "'sua'")
            {
                foreach (RepeaterItem item in rptA.Items)
                {
                    CheckBox ch = item.FindControl("ChBox") as CheckBox;
                    HiddenField oField = item.FindControl("HdField") as HiddenField;
                    if (ch.Checked)
                    { //Edit
                        Guid idStu = Guid.Parse(oField.Value.ToString());
                        DiemDanh obj = _DiemDanhRepository.GetDiemDanhBySchoolIdAndSchoolYearAndStudentId(Session["SchoolYearID"].ToString(),
                                                                                                      int.Parse(Session["SchoolID"].ToString()), idStu.ToString());
                        _DiemDanhRepository.Update(new DiemDanh(obj.Id, Convert.ToDateTime(txtNgayDD.Text), idStu,
                            Guid.Parse(ddlKhoi.SelectedValue.ToString()), true, Guid.Parse(Session["SchoolYearID"].ToString())));
                    }
                    else
                    {
                        Guid idStu = Guid.Parse(oField.Value.ToString());
                        DiemDanh obj = _DiemDanhRepository.GetDiemDanhBySchoolIdAndSchoolYearAndStudentId(Session["SchoolYearID"].ToString(),
                                                                                                     int.Parse(Session["SchoolID"].ToString()), idStu.ToString());

                        _DiemDanhRepository.Update(new DiemDanh(obj.Id, Convert.ToDateTime(txtNgayDD.Text), idStu,
                            Guid.Parse(ddlKhoi.SelectedValue.ToString()), false, Guid.Parse(Session["SchoolYearID"].ToString())));

                        vang++;
                    }
                }
                if (vang == 0)
                {
                    Alert.Show("Không có trẻ nào váng mặt !");
                }
                else
                {
                    Alert.Show("Có " + vang.ToString() + " trẻ vắng mặt!");
                }



            }
            if (action == "'them'")
            {
                foreach (RepeaterItem item in rptA.Items)
                {
                    CheckBox ch = item.FindControl("ChBox") as CheckBox;
                    HiddenField oField = item.FindControl("HdField") as HiddenField;
                    if (ch.Checked)
                    { //AddNew
                        Guid idStu = Guid.Parse(oField.Value.ToString());
                        _DiemDanhRepository.Insert(new DiemDanh(Guid.NewGuid(), Convert.ToDateTime(txtNgayDD.Text), idStu,
                            Guid.Parse(ddlKhoi.SelectedValue.ToString()), true, Guid.Parse(Session["SchoolYearID"].ToString())));
                    }
                    else
                    {
                        Guid idStu = Guid.Parse(oField.Value.ToString());
                        _DiemDanhRepository.Insert(new DiemDanh(Guid.NewGuid(), Convert.ToDateTime(txtNgayDD.Text), idStu,
                            Guid.Parse(ddlKhoi.SelectedValue.ToString()), false, Guid.Parse(Session["SchoolYearID"].ToString())));

                        vang++;
                    }
                }
                if (vang == 0)
                {
                    Alert.Show("Không có trẻ nào váng mặt !");
                }
                else
                {
                    Alert.Show("Có " + vang.ToString() + " trẻ vắng mặt!");
                }


            }
            Alert.ClodePopup();
        }
        else
        {
            Alert.Show("Thông tin cần nhập chưa đầy đủ !");
        }

    }
}